<?php

    include '..\Models.php';
        
    
    DbConnector::Connect();
    
    
    //
    // DELETE PLACE
    //
    if(WebHelper::IsGetKeyNotNull("delete"))
    {
        $place = Place::Delete(WebHelper::GetInt("delete"));
        WebHelper::Redirect("/Admin/ListPlaces.php");        
    }
    
    //
    // GET PLACE ID
    //
    if(WebHelper::IsGetKeyNotNull("id"))
    {
        $id = WebHelper::GetInt("id");
        $place = new Place($id);
        
    }   
    
        
    //
    // SAVE PLACE
    //
    if(WebHelper::IsPostBack())
    {        
        //
        // Check to update or create new
        //
        if(!WebHelper::IsPostKeyNotNull("id"))
        {
            // Save the place
            
            $place = new Place();
        }
        else
        {
            $id = WebHelper::PostInt("id");
            $place = new Place($id);
        }
        
        
        
        $place->name = WebHelper::PostString("Name");
        $place->description = WebHelper::PostString("Description");
        $place->address = WebHelper::PostString("Address");
        $place->direction = WebHelper::PostString("Direction");
        $place->featured = WebHelper::IsPostKeyNotNull("Featured");   
        $place->lat = WebHelper::PostFloat("Lat");
        $place->lon = WebHelper::PostFloat("Lon");
        $place->tag = WebHelper::PostString("Tag");
        //$place->scoreTotal = WebHelper::PostInt("ScoreTotal");
        //$place->scoreNumberPeople = WebHelper::PostInt("ScoreNumberPeople");
                  
        $place->Save();       
        
            
        WebHelper::Redirect("/Admin/ListPlaces.php");       
    }
    
    function Checked($bitVal)
    {
        if($bitVal == "")
            return "checked";
        
        return "";
    }

?>

<html>
    <head>
        <script src="../_script/jsLib.js" language="javascript" type="text/javascript">            
    </head>
           
    <body>
    <a href="ListPlaces.php">Places</a> > <?=$place->name?><br><br>
    
    <h1><? echo($place->name) ?></h1><br>
    
    <form action="SubmitPlace.php" method="post">
        
        <input type="hidden" id="_ispostback" name="_ispostback" value="anyvalue" />
        <input type="hidden" id="id" name="id" value="<?= $place->id ?>" />
        
        <label for="Name">Name:</label> <input type="text" id="Name" name="Name" value="<?= $place->name ?>" /><br>
        <label for="Name">Description:</label> <textarea id="Description" name="Description"><?= $place->description ?></textarea><br>
        <label for="Name">Address:</label> <textarea id="Address" name="Address"><?= $place->address ?></textarea><br>
        <label for="Name">Direction:</label> <textarea id="Direction" name="Direction"><?= $place->direction ?></textarea><br>
        <label for="Name">Featured:</label> <input type="checkbox" id="Featured" name="Featured" <?= Checked($place->featured) ?> onclick="OnCheckBoxClicked(this)"; value="false" /><br>        
        <label for="Name">Lat.:</label> <input type="text" id="Lat" name="Lat" value="<?= $place->lat ?>" /><br>
        <label for="Name">Lan.:</label> <input type="text" id="Lon" name="Lon" value="<?= $place->lon ?>" /><br>
        <label for="Name">Tag:</label> <input type="text" id="Tag" name="Tag" value="<?= $place->tag ?>" /><br>
        
        <input type="SUBMIT" value="Submit" />
    </form>
    

    <?
    
    if($id > 0)
    {
    ?>
    <h2>Photos</h2>
    <br>
    <?
        $result = $place->GetPhotos();
        
        for ($i=0; $i < count($result); $i++)
        {
    ?>
           <img src="<?= AppConfig::$mediaUrl . $result[$i]->fileName; ?>"></a>
    <?
        }
    ?>
        <br>
        <a href="upload.php?id=<?=$id?>&type=photo">Upload Place Photo</a>
    
        <img src="<?= AppConfig::$mediaUrl . $place->GetLogo()->fileName?>" /><a href="upload.php?id=<?=$id?>&type=logo">Upload Logo</a> 
    <?
        DbConnector::Disconnect();
    }
    ?>
    </body>
    
</html>